﻿//   Copyright 2007-2011 Comdiv (F. Sadykov) - http://code.google.com/u/fagim.sadykov/
//   Supported by Media Technology LTD 
//    
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//    
//        http://www.apache.org/licenses/LICENSE-2.0
//    
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//   MODIFICATIONS HAVE BEEN MADE TO THIS FILE
//
////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Product          : QWeb JS library (QJS)
// Component        : QJS Render
// Description      : DOM Html builder for JSON
// Usage            : if object is formed with special names - render can built DOM for it, lazy funcions and Event binding supported
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

((function () {
    if (!qweb) {
        throw "qweb is not launched";
    }
    qweb.defineGlobal('qweb.render');

    qweb.render._renderImpl = Class.create();
	window.$$RO =  function(obj){
		return qweb.render.extend(obj);
	};
    
    Object.extend(qweb.render, {
        create: function (obj) {
            if (!obj) throw "need render options on create";
            return new qweb.render._renderImpl(obj);
        },
		
		extend : function(obj){
			obj = obj || {};	
			if(obj){
				Object.extend(obj,{
					sa : function(n,v){
						this.a = this.a || {};
						if(!this.a[n]){
							this.a[n] = "";
						}
						if(typeof(v)!="undefined"){
							this.a[n] = v;
						}
						return this;
					},
					sn : function(n){
						this.n = n;
						return this;
					},
					sc : function(cls){
						this.c = cls;
						return this;
					},
					se : function(e,enter){
						e = qweb.render.extend(e);
						if(e){
								
							this.e = this.e || [];
							this.e.push(e);
						}
						if(enter){
							if(typeof(enter)=="function"){
								enter(e);
								return this;
							}else{
								return e;
							}
						}else{
							return this;
						}
					},
					so : function(n,func){
						if(n && func){
							this.o = this.o ||{};
							this.o[n] = func;
						}
						return this;
					},
					st : function(t){
						this.t = t;
						return this;
					}
                });
			}
			return obj;
		}
    });
    Object.extend(qweb.render._renderImpl.prototype, {
        initialize: function (obj) {
			this.obj = obj;
			
        },
		
		render : function(target,id,type){	
			id = id || "";
			var el = null;
			
			if(Object.isArray(this.obj)){
				el = new Element("div",{id:id || "" ,'class':'qweb_array '+id||""});
				for(var i=0;i<this.obj.length;i++){
					el.appendChild(this._render(this.obj[i],id+'_i'));
				}
			}else{
				el = this._render(this.obj,id,true);
			}
			if(target){
				target = $(target);
				if(!type)type="replace";
				if(type=="replace"){
					target.update("");
				}
				target.appendChild(el);
			}
			return el;
		},
		
		
		
		_render : function(obj,id,idanyway){
			_id = id || "";
			_id +=  ( obj.id || "");
			if(_id==id && !idanyway){
				_id="";
			}else{
				id = _id;
			}
			var e = new Element (obj.n);
			if(_id){
				e.id = _id;
				e.setAttribute('id',_id);
			}
			if(obj.a){
			
				for(var n in obj.a){
					v = obj.a[n];
					if(v){
						e.setAttribute(n,v);
					}
				}
			}
			
			if(obj.c){
				cs = obj.c.match(/\w+/g);
				for(i=0;i<cs.length;i++){
					e.addClassName(cs[i]);
				}
			}
			if(obj.t){
				e.update(obj.t);
			}
			if(obj.e){
				if(Object.isArray(obj.e)){
				for(var i=0;i<obj.e.length;i++){
					e.appendChild(this._render(obj.e[i],id));
				}}else{
					e.appendChild(this._render(obj.e,id));
				}
			}
			if(obj.o){
				for(var n in obj.o){
					Event.observe(e,n,obj.o[n].bind(e));
				}
			}
			
			return e;
		}
		
		
    });


})());